//
//  MainViewController.swift
//  Sina
//
//  Created by 管观荣 on 15/9/21.
//  Copyright © 2015年 CHer. All rights reserved.
//

import UIKit


//设置TabBarController
class MainViewController: UITabBarController {

    
    //MARK: - 监听发布按钮
    @objc private func composeClick(){
        print("点我了")
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tabBar.backgroundImage = UIImage(named: "tabbar_background")
        //设置子控件
        addChildViewControllers()
        
        //设置发布按钮
        setupComposeBtn()
    }
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(true)
        //方式一
        tabBar.bringSubviewToFront(composeBtn)
        
        //方式二：这个方法直接写这里，上面这个方法就不需要
//        //设置发布按钮
//        setupComposeBtn()
    }
    //添加发布按钮
    private func setupComposeBtn(){
        //添加到Tabar上
        tabBar.addSubview(composeBtn)
        
        let count = self.childViewControllers.count
        let btW = view.bounds.width / CGFloat(count) - 1
        //CGRectInset：参考dx，dy，进行缩放或者放大
        composeBtn.frame = CGRectInset(tabBar.bounds, btW*2, 0)
        
        composeBtn.addTarget(self, action: "composeClick", forControlEvents: .TouchUpInside)
        
        
    }
    
    //添加全部子控件
    private func addChildViewControllers() {
        addChildViewController(HomeViewController(),title:"首页",imageName:"tabbar_home")
        
        addChildViewController(DiscoverViewController(),title:"发现",imageName:"tabbar_discover")
        //发布控制器
        addChildViewController(UIViewController())
        
        addChildViewController(MessageViewController(),title:"消息",imageName:"tabbar_message_center")
    addChildViewController(ProfileViewController(),title:"我",imageName:"tabbar_profile")
    }
    
    ///添加一个控制器（有标题，按钮图片）
    private func addChildViewController(childController: UIViewController,title:String,imageName:String) {
        
        childController.title = title
        
        childController.tabBarItem.image = UIImage(named: imageName)
        //
//         tabBar.tintColor = UIColor.orangeColor()
        //导航
        let nav = UINavigationController(rootViewController: childController)
        
        addChildViewController(nav)
        
    }
    ///懒加载
    private lazy var composeBtn:UIButton = {
        let bt = UIButton()
        bt.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        bt.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        bt.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        bt.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        
        return bt
    }()
}
